﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAccess;
using EPiServer.Security;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PageTypeBuilder;
using TemplateFoundation.Core;
using TemplateFoundation.Tests.PageTypes;

namespace TemplateFoundation.Tests.UnitTests.Core
{
    [TestClass]
    public class PageFactoryGlobalization
    {
        [TestMethod]
        public void TestGettingOnlyPublishedLanguageVersions()
        {
            var page = DataFactory.Instance.GetDefaultPageData(PageReference.RootPage, (int)PageTypeResolver.Instance.GetPageTypeID(typeof (StandardPage)),new LanguageSelector("en"));

            page.PageName = "My page";

            DataFactory.Instance.Save(page, SaveAction.Publish, AccessLevel.NoAccess);

            var savedPage = new PageReference(page.PageLink.ID).GetPage();

            // Create three published language versions
            PageFactory.Instance.CreateLanguageBranch(savedPage, "sv");
            PageFactory.Instance.CreateLanguageBranch(savedPage, "no");
            PageFactory.Instance.CreateLanguageBranch(savedPage, "de");

            // Create three unpublished language versions
            PageFactory.Instance.CreateLanguageBranch(savedPage, "fi", true);
            PageFactory.Instance.CreateLanguageBranch(savedPage, "fr", true);
            PageFactory.Instance.CreateLanguageBranch(savedPage, "es", true);

            // Verify all language versions exist
            Assert.AreEqual(7, DataFactory.Instance.GetLanguageBranches(savedPage.PageLink).Count, "The total number of language versions wasn't 7 as expected");

            // Verify only four are published (master language plus three additional languages)
            Assert.AreEqual(4, PageFactory.Instance.GetPublishedLanguageBranches(savedPage.PageLink).Count, "The number of published languages wasn't 4 as expected");
        }
    }
}
